<?php

namespace app\admin\controller;

use app\admin\model\system\SystemAdmin;
use app\admin\model\system\SystemMenus;
use app\admin\model\system\SystemRole;
use basic\BaseModel;
use think\facade\Route as Url;

/**
 * @title 权限控制 | 基类 所有控制器继承的类
 * Class AuthController
 * @package app\admin\controller
 */
class AuthController extends SystemBasic
{
    /**
     * 当前登录管理员的信息
     * @var SystemAdmin
     */
    protected $adminInfo;

    /**
     * 商户信息
     * @var array
     */
    protected $wxappInfo;

    protected $wxappId;
    /**
     * 当前登录管理员的ID
     * @var
     */
    protected $adminId;

    /**
     * 当前管理员的权限
     * @var array
     */
    protected $auth = [];


    /**
     * 跳过的控制器的名称
     * @var array
     */
    protected $skipLogController = ["index", "common"];

    /**
     * @title 初始化
     * @throws \Exception
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        if (!SystemAdmin::hasActiveAdmin()) return $this->redirect(Url::buildUrl('login/index')->suffix(false)->build());
        try {
            $adminInfo = SystemAdmin::activeAdminInfoOrFail();
        } catch (\Exception $e) {
            return $this->failed(SystemAdmin::getErrorInfo($e->getMessage()), Url::buildUrl('login/index')->suffix(false)->build());
        }
        $this->adminInfo = $adminInfo;
        $this->adminId = $adminInfo["id"];
        BaseModel::bindWxappId($adminInfo["wxapp_id"]);
        $this->wxappId = $adminInfo["wxapp_id"];
        $this->getActiveAdminInfo();
        $this->auth = SystemAdmin::activeAdminAuthOrFail();
        $this->wxappInfo = SystemAdmin::activeAdminAuthOrFail();
        $this->adminInfo->level === 0 || $this->checkAuth();
        $this->assign("_admin", $this->adminInfo);
        $type = "system";
        event("AdminVisit", [$this->adminInfo, $type]);
    }

    /**
     * 获取wxapp_id
     * @return mixed
     */
    public function getWxappId()
    {
        return $this->wxappId;
    }

    /**
     * @title 权限验证
     * @param null $action
     * @param null $controller
     * @param null $module
     * @param array $route
     * @return bool
     */
    protected function checkAuth($action = null, $controller = null, $module = null, array $route = [])
    {
        static $allAuth = null;
        if ($allAuth === null) $allAuth = SystemRole::getAllAuth();
        if ($module === null) $module = app('http')->getName();
        if ($controller === null) $controller = $this->request->controller();
        if ($action === null) $action = $this->request->action();
        if (!count($route)) $route = $this->request->route();
        if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
        $nowAuthName = SystemMenus::getAuthName($action, $controller, $module, $route);
        $baseNowAuthName = SystemMenus::getAuthName($action, $controller, $module, []);
        if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && !in_array($baseNowAuthName, $this->auth)))
            exit($this->failed('没有权限访问!'));
        return true;
    }

    /**
     * 获得当前用户最新信息
     * @return SystemAdmin
     * @throws \Exception
     */
    protected function getActiveAdminInfo()
    {
        $adminId = $this->adminId;
        $adminInfo = SystemAdmin::getValidAdminInfoOrFail($adminId);
        if (!$adminInfo) $this->failed(SystemAdmin::getErrorInfo('请登陆!'));
        $this->adminInfo = $adminInfo;
        SystemAdmin::setLoginInfo($adminInfo);
        return $adminInfo;
    }
}